<?php
namespace app\home\event;
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2016/10/29
 * Time: 16:55
 */
class Poster
{
    public function index($id,$num=1,$city=0){
        $num = $num > 5 ? 5 : $num;
        if($num == 1){
           return $this->getOne($id);
        }else{
            $map['spaceid'] = $id;
            $map['startdate'] = ['lt',time()];
            $map['enddate']   = ['gt',time()];
            $map['status']    = 1;
            $map['city']      = $city ? $city : $this->getCityInfo();
            $space = $this->getSpaceInfo($id);
            if($space->getData('type') == 'couplet'){
                return $this->getOne($id);
            }
            $obj = model('poster');
            $count = $obj->where($map)->count();
            if($count == 0){
                unset($map['city']);
            }
            $list = $obj->where($map)->field('id,name,type,setting')->order('ordid asc,id desc')->limit($num)->select();
            $temp = [];
            if($list){
                foreach($list as $v){
                    switch($v->getData('type')){
                        case 'flash':
                            $str = $this->flash($v['setting'],$space);
                            break;
                        case 'images':
                            $str = $this->images($v,$space);
                            break;
                        case 'text':
                            $str = $this->text($v);
                            break;
                        case 'code':
                            $str = $this->code($v);
                            break;
                        default:
                            $str = $this->images($v,$space);
                            break;
                    }
                    $temp[] = $str;
                }
            }
            return $temp;
        }
    }
    //对联广告只取一个一次
    public function getOne($id,$city=0){
        $map['spaceid']   = $id;
        $map['startdate'] = ['lt',time()];
        $map['enddate']   = ['gt',time()];
        $map['status']    = 1;
        $map['city']      = $city ? $city : $this->getCityInfo();
        $obj = model('poster');
        $count = $obj->where($map)->count();
        if($count == 0){
            unset($map['city']);
        }
        $info = $obj->where($map)->field('id,name,spaceid,type,setting')->order('ordid asc,id desc')->find();
        $str  = '';
        if($info){
            $space = $this->getSpaceInfo($info['spaceid']);
            switch($space->getData('type')){
                case 'couplet':
                    $str = $this->couplet($info,$space);
                    break;
                case 'text':
                    $str = $this->text($info);
                    break;
                case 'code' :
                    $str = $this->code($info);
                    break;
                case 'imagelist':
                    $str = $this->images($info,$space);
                    break;
                case 'banner':
                    $str = $this->banner($info,$space);
                    break;
                default:
                    $str = $this->images($info,$space);
                    break;
            }
        }
        return $str;
    }
    private function flash($data,$info){
            $flash = '<a href="'.$data['linkurl'].'" target="_blank"><object width="'.$info['width'].'px" height="'.$info['height'].'px" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">';
            $flash .= ' <param value="'.$data['fileurl'].'"  name="movie"><param value="high" name="quality">';
            $flash .= '<param value="transparent" name="wmode"><param value="8.0.35.0" name="swfversion">';
            $flash .= '<param value="/static/js/expressInstall.swf" name="expressinstall">';
            $flash .= '<!--[if !IE]>--><object width="'.$info['width'].'px" height="'.$info['height'].'px" data="';
            $flash .= $data['fileurl'].'" type="application/x-shockwave-flash"><!--<![endif]--><param value="high" name="quality">';
            $flash .= '<param value="transparent" name="wmode"><param value="8.0.35.0" name="swfversion">';
            $flash .= '<param value="/static/js/expressInstall.swf" name="expressinstall">';
            $flash .= '<div>您的浏览器<br>需要较新版本的<a href="http://www.adobe.com/go/getflashplayer">Adobe Flash Player</a>才能显示此处内容</div><!--[if !IE]>--></object><!--<![endif]--></object></a>';
            return $flash;
    }
    //对联广告
    private function couplet($data,$info){
        if($data->getData('type') == 'flash'){
            $str_left  = $this->flash($data['setting']['left'],$info);
            $str_right = $this->flash($data['setting']['right'],$info);
        }else{
            $str_left   = '<a href="'.$data['setting']['left']['linkurl'].'" target="_blank"><img alt="'.$data['setting']['left']['alt'].'" src="'.$data['setting']['left']['fileurl'].'" width="'.$info['width'].'" height="'.$info['height'].'px"/></a>';
            $str_right  = '<a href="'.$data['setting']['left']['linkurl'].'" target="_blank"><img alt="'.$data['setting']['right']['alt'].'" src="'.$data['setting']['right']['fileurl'].'" width="'.$info['width'].'" height="'.$info['height'].'px" /></a>';
        }
        $str = '<script src="/static/home/js/couplet.js"></script><script>';
        //左面
        $str .= "theFloaters.addItem('followDiv1',".$info['setting']['paddleft'].",".$info['setting']['paddtop'].",".$info['width'].",".$info['height'].",'left','".$str_left."');";
        //右面
        $str .= "theFloaters.addItem('followDiv2',".$info['setting']['paddleft'].",".$info['setting']['paddtop'].",".$info['width'].",".$info['height'].",'right','".$str_right."');";
        $str .= "theFloaters.play();</script>";
        return $str;
    }
    //文字广告
    private function text($data){
        $str = "<a href='".$data['setting']['linkurl']."' title='".$data['setting']['title']."' target='_blank'>".$data['setting']['title']."</a>";
        return $str;
    }
    //代码广告
    private function code($data){
        return $data['setting']['code'];
    }
    private function images($data,$info){
        $str = "<a href='".$data['setting']['linkurl']."' title='".$data['setting']['alt']."' target='_blank'><img alt='".$data['setting']['alt']."' src='".$data['setting']['fileurl']."' width='100%'  /></a>";
        return $str;
    }
    private function banner($data,$info){
        if($data->getData('type') == 'flash'){
            $str   = $this->flash($data['setting'],$info);
        }else{
            $str   = $this->images($data,$info);
        }
        return $str;
    }
    private function getSpaceInfo($id){
        $info = model('poster_space')->where(['id'=>$id])->field('width,height,type,setting')->find();
        return $info;
    }
    private function getCityInfo(){
        $city = cookie('cityInfo');
        if(!$city){
            return 0;
        }
        return $city['id'];
    }
}